package com.forxindu.kit.box;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;

public class DateKit {

    public static final String PATTERN1 = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN2 = "yyyyMMddHHmmss";
    public static final String PATTERN3 = "yyyy-MM-dd";
    public static final String PATTERN4 = "yyyy年MM月dd日";
    public static final String PATTERN5 = "MM-dd HH:mm";
    public static final String PATTERN6 = "HH:mm";
    public static final String PATTERN7 = "yyyy年MM月";
    public static final String PATTERN8 = "HH:mm:ss";
    public static final String PATTERN9 = "yyyy-MM-dd HH:mm";

    /**
     * 一分钟的毫秒值
     */
    public final static long ONE_MINUTE_TIME = 60l * 1000l;
    /**
     * 一小时的毫秒值
     */
    public final static long ONE_HOURS_TIME = 60l * 60l * 1000l;
    /**
     * 一天的毫秒值
     */
    public final static long ONE_DAY_TIME = 24l * 60l * 60l * 1000l;

    /**
     * <b>stringConvertDateByRegx。</b>
     * <p>
     * <b>详细说明：</b>
     * </p>
     * <!-- 在此添加详细说明 --> 通过正则表达式将String日期转为Date对象。
     *
     * @param dateString
     * @param regx
     * @return
     */
    public static Date stringConvertDateByRegx(String dateString, String regx) {
        try {
            Pattern GRUP_PATTERN = Pattern.compile(regx);
            if (GRUP_PATTERN.matcher(dateString).lookingAt()) {
                dateString = dateString.substring(dateString.indexOf("(") + 1,
                        dateString.indexOf(")"));
                return new Date(Long.parseLong(dateString));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date stringConvertDateByPattern(String dateStr, String formatStr) {
        try {
            SimpleDateFormat dd = new SimpleDateFormat(formatStr, Locale.getDefault());
            return dd.parse(dateStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    /**
     * <b>dateConvertStringByYYYYMMDDHHMMSS。</b>
     * <p>
     * <b>详细说明：</b>
     * </p>
     * <!-- 在此添加详细说明 --> 日期对象按“yyyyMMddHHmmss”的模板转为String。
     *
     * @param date
     * @return
     */
    public static String dateConvertStringByYYYYMMDDHHMMSS(Date date) {
        String dateString = "";
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(PATTERN2, Locale.getDefault());
            dateString = formatter.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateString;
    }

    /**
     * <b>dateConvertStringByYYYYMMDDHHMMSS。</b>
     * <p>
     * <b>详细说明：</b>
     * </p>
     * <!-- 在此添加详细说明 --> 将当前日期对象按“yyyyMMddHHmmss”的模板转为String。
     *
     * @return
     */
    public static String getCurrentTimeByYYYYMMDDHHMMSS() {
        return dateConvertStringByYYYYMMDDHHMMSS(Calendar.getInstance()
                .getTime());
    }

    /**
     * <b>longStringConvertDate。</b>
     * <p>
     * <b>详细说明：</b>
     * </p>
     * <!-- 在此添加详细说明 --> 将字符型的日期毫秒数直接生成为日期对象。
     *
     * @param longTime
     * @return
     */
    public static Date longStringConvertDate(String longTime) {
        try {
            return new Date(Timestamp.valueOf(longTime).getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * <b>dateConvertCalendar。</b>
     * <p>
     * <b>详细说明：</b>
     * </p>
     * <!-- 在此添加详细说明 --> 将日期对象转为Calendar对象。
     *
     * @param date
     * @return
     */
    public static Calendar dateConvertCalendar(Date date) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * <b>dateConvertStringByPattern。</b>
     * <p>
     * <b>详细说明：</b>
     * </p>
     * <!-- 在此添加详细说明 --> 将日期对象按Pattern模板转为String串。
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String dateConvertStringByPattern(Date date, String pattern) {
        String dateString = "";
        if (date != null) {
            SimpleDateFormat format = new SimpleDateFormat(pattern,
                    Locale.getDefault());
            dateString = format.format(date);
        }
        return dateString;
    }
    /**
     * <b>dateConvertStringByPattern。</b>
     * <p>
     * <b>详细说明：</b>
     * </p>
     * <!-- 在此添加详细说明 --> 将日期对象按Pattern获取星期信息。
     *
     * @return
     */
    public static String getWeek(String pTime) {
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat(PATTERN3);
        Calendar calendar = Calendar.getInstance();
        try {

            calendar.setTime(format.parse(pTime));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            Week += "周天";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            Week += "周一";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
            Week += "周二";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
            Week += "周三";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
            Week += "周四";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
            Week += "周五";
        }
        if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            Week += "周六";
        }
        return Week;
    }

    public static String showTime(long time) {
        long tempTime = (System.currentTimeMillis() - time);
        if (tempTime > 0 && tempTime < ONE_MINUTE_TIME) {
            return "刚刚";
        } else if (tempTime > ONE_MINUTE_TIME && tempTime < ONE_HOURS_TIME) {
            int minute = (int) (tempTime % (60 * 60 * 1000) / (60 * 1000));
            return String.format("%1$s分钟前", minute);
        } else if (tempTime > ONE_HOURS_TIME && tempTime < ONE_DAY_TIME) {
            int hours = (int) (tempTime / (60 * 60 * 1000));
            return String.format("%1$s小时前", hours);
        } else {
            return DateKit.dateConvertStringByPattern(new Date(time), DateKit.PATTERN3);
        }
    }

    public static long dateToMinute(long time) {
        return time / ONE_MINUTE_TIME;
    }

    public static long dateToDay(long time) {
        return time / ONE_DAY_TIME;
    }

    /**
     * 获取毫秒数
     */
    public static String getTime() {
        String timeStamp = "";
        Date date = new Date();
        timeStamp = String.valueOf(date.getTime());
        return timeStamp;
    }

    /**
     * 获取秒数
     */
    public static String getSecond() {
        String timeStamp = "";
        Date date = new Date();
        long time = date.getTime();
        long second = time/1000;
        timeStamp = String.valueOf(second);
        return timeStamp;
    }

}